/**
 * 
 */
package com.teamall.web.sjtj.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.DateUtils;
import com.teamall.framework.util.Utils;
import com.teamall.web.base.db.po.TownInfoPO;
import com.teamall.web.base.service.ITownInfoService;
import com.teamall.web.sjtj.service.IProjectSummaryService;

/**
 * <Description>精准施策 <br>
 * 
 * @author pan.xiaobo<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2017年9月12日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.sjtj.controller <br>
 */
@Controller
@RequestMapping("/jzsc")
public class JzscController {

    @Autowired
    private ITownInfoService townInfoService;

    @Autowired
    private IProjectSummaryService projectSummaryService;

    /**
     * 项目汇总-界面
     * 
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("/projectSummaryIndex")
    public ModelAndView projectSummaryIndex() throws BaseAppException {
        ModelAndView modelAndView = new JModelAndView(
                "/sjtj/jzsc_project_summary");
        List<TownInfoPO> townInfoPOList = townInfoService
                .selectByArg(new TownInfoPO());

        modelAndView.addObject("townInfoPOList", townInfoPOList);
        modelAndView.addObject("currentYear", DateUtils.date2String(new Date(),
                DateUtils.STR_DATE_FORMAT_YEAR));

        return modelAndView;
    }

    /**
     * 获取饼图数据
     * 
     * @param vtime
     * @param townId
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("/qryPieData")
    @ResponseBody
    public Map<String, Object> qryPieData(Integer vtime, Integer townId)
            throws BaseAppException {
        if (Utils.isEmpty(vtime)) {
            vtime = Integer.parseInt(DateUtils.date2String(new Date(),
                    DateUtils.STR_DATE_FORMAT_YEAR));
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (townId != null && townId == -1) {
            paramMap.put("vtime", vtime);
            Map<String, Object> pieData1Map = projectSummaryService
                    .selectPieData1ByAllTown(paramMap);
            Map<String, Object> pieData2Map = projectSummaryService
                    .selectPieData2ByAllTown(paramMap);
            resultMap.putAll(pieData1Map);
            resultMap.putAll(pieData2Map);
        }
        else if (townId != null && townId > 0) {
            paramMap.put("vtime", vtime);
            paramMap.put("townId", townId);
            Map<String, Object> pieData1Map = projectSummaryService
                    .selectPieData1ByOneTown(paramMap);
            Map<String, Object> pieData2Map = projectSummaryService
                    .selectPieData2ByOneTown(paramMap);
            resultMap.putAll(pieData1Map);
            resultMap.putAll(pieData2Map);
        }
        return resultMap;
    }

    /**
     * 累计投入走势
     * 
     * @param vtime
     * @param townId
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("/qryLineData")
    @ResponseBody
    public List<Map<String, Object>> qryLineData(Integer vtime, Integer townId)
            throws BaseAppException {
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        if (Utils.isEmpty(vtime)) {
            vtime = Integer.parseInt(DateUtils.date2String(new Date(),
                    DateUtils.STR_DATE_FORMAT_YEAR));
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (townId != null && townId == -1) {
            paramMap.put("vtime", vtime);
            List<Map<String, Object>> lineDataList = projectSummaryService
                    .selectlineData2ByAllTown(paramMap);
            resultList.addAll(lineDataList);
        }
        else if (townId != null && townId > 0) {
            paramMap.put("vtime", vtime);
            paramMap.put("townId", townId);
            List<Map<String, Object>> lineDataList = projectSummaryService
                    .selectlineData2ByOneTown(paramMap);
            resultList.addAll(lineDataList);
        }
        return resultList;
    }

    /**
     * 项目列表
     * 
     * @param vtime
     * @param townId
     * @return
     * @throws BaseAppException
     */
    @RequestMapping("/qryGridData")
    @ResponseBody
    public Page<Map<String, Object>> qryGridData(Integer vtime, Integer townId,
            Page<Map<String, Object>> resultPage) throws BaseAppException {
        if (Utils.isEmpty(vtime)) {
            vtime = Integer.parseInt(DateUtils.date2String(new Date(),
                    DateUtils.STR_DATE_FORMAT_YEAR));
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (townId != null && townId == -1) {
            paramMap.put("vtime", vtime);
            projectSummaryService
                    .selectGridData2ByAllTown(paramMap, resultPage);
        }
        else if (townId != null && townId > 0) {
            paramMap.put("vtime", vtime);
            paramMap.put("townId", townId);
            projectSummaryService
                    .selectGridData2ByOneTown(paramMap, resultPage);
        }
        return resultPage;
    }
}
